package com.diandiansong.app;

import android.app.Application;
import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;

import com.diandiansong.app.entity.CommodityInfoInfo;
import com.diandiansong.app.entity.LoginInfo;
import com.diandiansong.app.event.NEWS;
import com.diandiansong.app.event.SECOND;
import com.diandiansong.app.ui.index.BuyInItem;
import com.diandiansong.app.utils.Api;
import com.diandiansong.app.widgets.AddAndMinusViewInItem;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;

import diandiansong.library.utils.ImgLoader;

import static com.diandiansong.app.ui.index.BuyInItem.*;
import static diandiansong.library.ui.base.BaseListAdapter.fastText;
import static diandiansong.library.ui.base.BaseListAdapter.findViewById;

public class App extends Application {

    public static boolean DEBUG = true;

    static App mApp;

    public static App getApplication() {
        return mApp;
    }

    public static App getContext() {
        return mApp;
    }

    int mScondCount;

    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;

        Api.TOKEN = LoginInfo.getToken(this);

        mSecondTimer = new CountDownTimer(Long.MAX_VALUE, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mScondCount++;

                EventBus.getDefault().post(new SECOND());


//                if (mScondCount % 30 == 0) {
//                    EventBus.getDefault().post(new KILL_REFRESH());
//                }

                if (mScondCount % 5 == 0) {
                    EventBus.getDefault().post(new NEWS());
                }

            }

            @Override
            public void onFinish() {
            }
        }.start();
    }

    public static float readable(float f) {
        int scale = 2;//设置位数爱爱啊
        int roundingMode = 4;//表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
        BigDecimal bd = new BigDecimal((double) f);
        bd = bd.setScale(scale, roundingMode);
        f = bd.floatValue();
        return f;
    }

    public static boolean isUserClient() {
        return !getApplication().getPackageName().endsWith("manager");
    }

    CountDownTimer mSecondTimer;


    ///////////////////////////////////////////////////////////////////////////
    // good list
    ///////////////////////////////////////////////////////////////////////////
///
    public static void initGoodList(final Context context, final CommodityInfoInfo.Data item, int position, View convertView) {
        ImageView iv = findViewById(convertView, R.id.iv_logo);
        ImgLoader.display(iv, item.getImgs());

        fastText(convertView, R.id.tv_1, item.getCommodityName());
        fastText(convertView, R.id.tv_2, item.getCommoditySubtitle());

        String unit = item.getUnit();
        if (unit == null || unit.isEmpty() || unit.equals("/")) {
            unit = "";
        } else {
            unit = "/" + unit;
        }

        fastText(convertView, R.id.tv_3, "￥" + item.getPrice() + unit);

        AddAndMinusViewInItem mAddMinus = findViewById(convertView, R.id.add_minus);
        mAddMinus.setAddListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(context, item.getCommodityId());
            }
        });
    }

    public static void initGoodListAdd(final Context context, final String commodityId, View convertView) {
        AddAndMinusViewInItem mAddMinus = findViewById(convertView, R.id.add_minus);
        mAddMinus.setAddListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(context, commodityId);
            }
        });
    }
}
